#!/usr/bin/env node

//通过响应头来传递数据，把数据暴露给了客户
const Koa = require('koa');
const log = console.log;
const app = new Koa();
app.listen(8080);

app.use((ctx, next) => {  //M-logger
    log(`${ctx.method} ${ctx.path}`);
    next();//调下一个
    const cost = ctx.response.get('cost');
    log(`cost: ${cost}ms`);
})

app.use((ctx, next) => {  //M-ms
    const start = Date.now();
    next();
    const end = Date.now();
    ctx.set('cost', end -start);
});

app.use((ctx, next) => {  //M-hello
    log('hello Koa!');
    ctx.body = 'ok';
})

